//
//  GoodsDetailsViewController.m
//  何五路
//
//  Created by _ADY on 15/6/9.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "GoodsDetailsViewController.h"
#import "MWCommon.h"
#import "UIButton+CustomBadge.h"
@interface GoodsDetailsViewController ()

@end
static LoadingView *loading = nil;

#define barTagButton 10
#define baraTagButton 20
#define barbTagButton 30
#define scrTag 100
#define sectionHight screenMySize.size.height
@implementation GoodsDetailsViewController
@synthesize myTableView,_photos,_thumbs,productId,allArray,mTableView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"商品详情";
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    toScrollview = 1;
    selectInt = 1;
    [self gotoGWCInt];
    
    if (allArray.count >0)
    {
        return;
    }
    self.navigationController.navigationBar.alpha = 0.0;

    loading = [[LoadingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:loading];
    
    allArray = [[NSDictionary alloc] init];
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:productId forKey:@"productId"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService GetDetailProduct:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [loading closeLoading];
            if (ret !=nil)
            {
                if ([[NSString stringWithFormat:@"%@",[ret objectForKey:@"Result"]] isEqualToString:@"error"]){
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[ret objectForKey:@"Message"]  delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alert show];
                    return ;
                }
                
               allArray =  [ret objectForKey:@"Entity"];
                NSLog(@"allArray %@",allArray);
                
                hdINT = 0;
                NSString *mStr = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"DisplayActivityInfo"]];
                if ([mStr hash] && ![mStr isEqualToString:@"<null>"])
                {
                    hdINT = 1;
                }

                
            }
            SKTView *SKJTable = [[SKTView alloc] initWithFrame:CGRectMake(0, 0, SLIP_WIDTH, screenMySize.size.height)];
            SKJTable.allArray = allArray;
            SKJTable.productId = productId;
            SKJTable.delegate = self;
            //侧栏
            _sideSlipView = [[JKSideSlipView alloc]initWithSender:self];
            _sideSlipView.backgroundColor = [UIColor clearColor];
            [_sideSlipView setContentView:SKJTable];
            [[UIApplication sharedApplication].keyWindow addSubview:_sideSlipView];
            
            
            NSMutableArray *apArray = [[NSMutableArray alloc] init];
            if ([[allArray allKeys] containsObject:@"ProductAttributes"])
            {
                for (NSDictionary *pic1 in [allArray objectForKey:@"ProductAttributes"])
                {
                    if ([[pic1 allKeys] containsObject:@"Values"])
                    {
                        [apArray addObject:pic1];
                    }
                }
            }
            
            myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenMySize.size.width, screenMySize.size.height-60)];
            myTableView.delegate = self;
            myTableView.dataSource = self;
            myTableView.backgroundColor = [UIColor clearColor];
            [taScrollView addSubview:myTableView];
            
            
            mTableView = [[EvaluationTableViewController alloc] initWithStyle:UITableViewStylePlain];
            mTableView.view.frame = CGRectMake(0, screenMySize.size.height-10, screenMySize.size.width, screenMySize.size.height-110);
            mTableView.delegate = self;
            mTableView.view.backgroundColor = [UIColor whiteColor];
            mTableView.allArray = allArray;
            mTableView.productId = productId;
            [taScrollView addSubview:mTableView.view];
            
            [self returnSelected:1 setNSM:apArray];
            [myTableView reloadData];
        });
    });
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self popself];
}

- (void)viewWillDisappear:(BOOL)animated
{
    toScrollview = 2;
    self.navigationController.navigationBar.alpha = 1;
    [super viewWillDisappear:animated];
}

-(void)rightAction
{
    
}

-(void)popself
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"home"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    
    taScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,  screenMySize.size.width, screenMySize.size.height-60)];
    taScrollView.backgroundColor = [UIColor clearColor];
    [taScrollView setDelegate:self];
    taScrollView.showsVerticalScrollIndicator = FALSE;
    taScrollView.showsHorizontalScrollIndicator = FALSE;
    [taScrollView setPagingEnabled:YES];
    [taScrollView setShowsHorizontalScrollIndicator:NO];
    [self.view addSubview:taScrollView];
    
    [taScrollView setContentSize:CGSizeMake(screenMySize.size.width,(screenMySize.size.height-60)*2)];
    
    
    [self toolbar];

    UIButton *bar1Button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    bar1Button.frame = CGRectMake(4, 27,27, 28);
    [bar1Button addTarget:self action:@selector(popself) forControlEvents:UIControlEventTouchUpInside];
    [bar1Button setBackgroundImage:[UIImage imageNamed:@"fhui"] forState:UIControlStateNormal];
    [self.view addSubview:bar1Button];
    
    UIButton *barButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    barButton.frame = CGRectMake(screenMySize.size.width-41, 29,25, 25);
    [barButton addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    [barButton setBackgroundImage:[UIImage imageNamed:@"home"] forState:UIControlStateNormal];
    [self.view addSubview:barButton];

}

-(void)toolbar
{
    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, screenMySize.size.height-60, screenMySize.size.width, 60)];
    barView.backgroundColor = [UIColor colorWithWhite:0 alpha:.7];
    [self.view addSubview:barView];
    
    for (int  i = 0; i < 3; i++)
    {
        UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i == 2)
            barButton.frame = CGRectMake(0, 0,screenMySize.size.width/3, 60);
        else
            barButton.frame = CGRectMake(0, 0,40, 40);
        barButton.center = CGPointMake(screenMySize.size.width/6+i*screenMySize.size.width/3, 30);
        [barButton addTarget:self action:@selector(barAction:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0)
            [barButton setBackgroundImage:[UIImage imageNamed:@"kf"] forState:UIControlStateNormal];
        else if (i == 1)
            [barButton setBackgroundImage:[UIImage imageNamed:@"gwc"] forState:UIControlStateNormal];
        else if (i == 2)
        {
            [barButton setTitle:@"购买" forState:UIControlStateNormal];
            [barButton setBackgroundColor:[UIColor colorWithRed:248/255.0 green:181/255.0 blue:0/255.0 alpha:1]];
             [barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        barButton.tag = barTagButton +i;
       
        barButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+3];
        [barView addSubview:barButton];
    }
    [self gotoGWCInt];

}

-(void)gotoGWCInt
{
    BOOL aLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLoginKey"];
    if (!aLogin) {
        return;
    }
    
    UIButton *gwcButton = (UIButton*)[self.view viewWithTag:barTagButton+1];
    [gwcButton setBadgeWithNumber:[NSNumber numberWithInt:[[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"ShoppingCartCountKey"]] intValue]]];
}

-(void)loginAction
{
    LoginViewController *browser = [[LoginViewController alloc] init];
    browser.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:browser animated:YES];
}

#pragma mark 客服、购物车、购买
-(void)barAction:(id)sender
{
    BOOL aLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLoginKey"];
    if (!aLogin) {
        [self loginAction];
        return;
    }
    
    int  type = ((UIButton*)sender).tag -barTagButton;
    if (type == 0) {
        KefWebViewController *vc = [[KefWebViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (type == 1)
    {
        ShoppingViewController *browser = [[ShoppingViewController alloc] init];
        browser.hidesBottomBarWhenPushed = YES;
        browser.aBarString = @"1";
        [self.navigationController pushViewController:browser animated:YES];
    }
    else if (type == 2)
    {
        [self gotoGouM:1];

    }
    
}

-(void)gotoGouM:(int)type
{
    NSMutableDictionary *postDic1 = [[NSMutableDictionary alloc] init];
    [postDic1 setObject:productId forKey:@"productId"];
    [postDic1 setObject:[NSString stringWithFormat:@"%d",type] forKey:@"shoppingCartTypeId"];
    if (type == 1) {
       [postDic1 setObject:[NSString stringWithFormat:@"%d",selectInt] forKey:[NSString stringWithFormat:@"addtocart_%@.EnteredQuantity",productId]];
    }
    
    
    NSMutableArray *apArray = [[NSMutableArray alloc] init];
    if ([[allArray allKeys] containsObject:@"ProductAttributes"])
    {
        for (NSDictionary *pic1 in [allArray objectForKey:@"ProductAttributes"])
        {
            if ([[pic1 allKeys] containsObject:@"Values"])
            {
                [apArray addObject:pic1];
            }
        }
    }
    if (selectArray.count != 0) {
        for (NSDictionary *pic1 in selectArray)
        {
            if ([[NSString  stringWithFormat:@"%@",[pic1 objectForKey:@"IsRequired"]] isEqualToString:@"1"])
            {
                if ([[pic1 allKeys] containsObject:@"Values"])
                {
                    BOOL ISNO = NO;
                    for (NSDictionary *pic in [pic1 objectForKey:@"Values"])
                    {
                        if ([[NSString stringWithFormat:@"%@",[pic objectForKey:@"IsPreSelected"]] isEqualToString:@"1"]) {
                            
                            ISNO = YES;
                            [postDic1 setObject:[NSString stringWithFormat:@"%@",[pic objectForKey:@"Id"]] forKey:[NSString stringWithFormat:@"product_attribute_%@_%@_%@",productId,[pic1 objectForKey:@"ProductAttributeId"],[pic1 objectForKey:@"Id"]]];
                        }
                    }
                    if (!ISNO) {
                        if ([[pic1 objectForKey:@"Values"] count] >=1) {
                            
                            ISNO = YES;
                            [postDic1 setObject:[NSString stringWithFormat:@"%@",[[[pic1 objectForKey:@"Values"] objectAtIndex:0] objectForKey:@"Id"]] forKey:[NSString stringWithFormat:@"product_attribute_%@_%@_%@",productId,[pic1 objectForKey:@"ProductAttributeId"],[pic1 objectForKey:@"Id"]]];
                        }
                        
                    }

                }
            }
        }
    }
    else
    {
        if (apArray.count != 0)
        {
            for (NSDictionary *pic1 in apArray)
            {
                if ([[NSString  stringWithFormat:@"%@",[pic1 objectForKey:@"IsRequired"]] isEqualToString:@"1"])
                {
                    if ([[pic1 allKeys] containsObject:@"Values"])
                    {
                        BOOL ISNO = NO;
                        for (NSDictionary *pic in [pic1 objectForKey:@"Values"])
                        {
                            if ([[NSString stringWithFormat:@"%@",[pic objectForKey:@"IsPreSelected"]] isEqualToString:@"1"]) {
                                
                                [postDic1 setObject:[NSString stringWithFormat:@"%@",[pic objectForKey:@"Id"]] forKey:[NSString stringWithFormat:@"product_attribute_%@_%@_%@",productId,[pic1 objectForKey:@"ProductAttributeId"],[pic1 objectForKey:@"Id"]]];
                            }
                        }
                        if (!ISNO) {
                            if ([[pic1 objectForKey:@"Values"] count] >=1) {
                                
                                 ISNO = YES;
                                [postDic1 setObject:[NSString stringWithFormat:@"%@",[[[pic1 objectForKey:@"Values"] objectAtIndex:0] objectForKey:@"Id"]] forKey:[NSString stringWithFormat:@"product_attribute_%@_%@_%@",productId,[pic1 objectForKey:@"ProductAttributeId"],[pic1 objectForKey:@"Id"]]];
                            }
                            
                        }
                    }
                }
            }
        }
    }

    loading = [[LoadingView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:loading];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *myArray = [SendService AddProductToCart_DetailsTwo:postDic1];
        dispatch_async(dispatch_get_main_queue(), ^{
//            NSLog(@"myarray %@",myArray);
            if ([[NSString stringWithFormat:@"%@",[myArray objectForKey:@"Message"]] isEqualToString:@"<null>"]) {
                [SendService alerViewMessage:[[myArray objectForKey:@"Entity"]objectForKey:@"Message"]];

            }
            else
            {
                [SendService alerViewMessage:[myArray objectForKey:@"Message"]];
                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
                    NSDictionary *ret = [SendService GetShoppingCartCount];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        
                        if (ret !=nil)
                        {
                            if ([[ret objectForKey:@"Result"] isEqualToString:@"success"])
                            {
                                [[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithFormat:@"%@",[[ret objectForKey:@"Entity"] objectForKey:@"ShoppingCartCount"]] forKey:@"ShoppingCartCountKey"];
                                NSArray* viewConrolers = [self.tabBarController viewControllers];
                                ShoppingViewController* vc = [viewConrolers objectAtIndex:2];
                                vc.tabBarItem.badgeValue = [NSString stringWithFormat:@"%@",[[ret objectForKey:@"Entity"] objectForKey:@"ShoppingCartCount"]];
                                [self gotoGWCInt];
                                
                            }
                        }
                        
                    });
                });
            }
            [loading closeLoading];
        });
    });
}
#pragma mark 分享等
-(void)baraAction:(id)sender
{
    int  type = ((UIButton*)sender).tag -baraTagButton;
    if (type == 0) {
        [self gotoGouM:2];
    }
    else if (type == 1)
    {
        if (aCellShareView.alpha)
            aCellShareView.alpha = 0;
        else
            aCellShareView.alpha = 1;
    }
    
}

#pragma mark 分享
-(void)barbAction:(id)sender
{
//    int  type = ((UIButton*)sender).tag -barbTagButton;
    
    
}

-(void)returnPage
{
    [taScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    [myTableView setContentOffset:CGPointMake(0, 0) animated:YES];
    taScrollView.delegate = self;
    taScrollView.scrollEnabled = YES;
}
-(void)setController:(UIViewController*)controller
{
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (toScrollview == 2) {
        return;
    }
    else if (toScrollview == 1 && scrollView.contentOffset.y<0)
    {
        [taScrollView setContentOffset:CGPointMake(0,0) animated:YES];
        toScrollview = 0;
    }
    
    CGFloat offset=scrollView.contentOffset.y;
    if (offset>(screenMySize.size.height*3/5-40)) {

    }
    else if (offset<0)
    {
        self.navigationController.navigationBar.alpha = 0.0;
    }else
    {
        self.navigationController.navigationBar.alpha = offset/(screenMySize.size.height*3/5-40);
    }
    aCellShareView.alpha = 0;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if ([scrollView isKindOfClass:[UITableView class]]) {
        return;
    }
    
    int page =scrollView.bounds.origin.x/self.view.bounds.size.width;
    if (scrollView == taScrollView)
    {
        if (scrollView.contentOffset.y<0)
        {
            [taScrollView setContentOffset:CGPointMake(0,0) animated:YES];
        }
        int page1 =scrollView.bounds.origin.y/(self.view.bounds.size.height-40);
        if (page1 == 1) {
            self.navigationController.navigationBar.alpha =1;
            taScrollView.delegate = nil;
            taScrollView.scrollEnabled = NO;
        }
    }
    else if (scrollView.tag == scrTag)
    {
        UIPageControl *pageControl = (UIPageControl*)[self.view viewWithTag:scrTag+1];
        [pageControl setCurrentPage:page] ;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
    return 6+hdINT;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        if (indexPath.row == 0)
            return screenMySize.size.height*3/5;
        else if (indexPath.row == 1)
            return 60;
        else if (indexPath.row == 2)
        {
            int hight = 0;
            NSString *mStr = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ShortDescription"]];
            if ([mStr hash] && ![mStr isEqualToString:@"<null>"])
            {
                hight = [SendService setString:mStr setSize:labelSize-1 setHight:screenMySize.size.width-40];
            }
            return 30+hight;
        }
        else if (indexPath.row == 3)
            return 50;
        else if (indexPath.row == 4)
        {
            int hight = 0;
            NSString *mStr = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"DisplayActivityInfo"]];
            if ([mStr hash] && ![mStr isEqualToString:@"<null>"])
            {
                hight = [SendService setString:mStr setSize:labelSize-1 setHight:self.view.bounds.size.width-70];
                if (hight < 30) {
                    hight = 30;
                }
                return hight;
            }
            return 35;
        }
        else if (indexPath.row == 4+hdINT)
            return 35;
        else if (indexPath.row == 5+hdINT)
            return 60+60;
    }
    return YES;
}

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    if (allArray.count == 0) {
        return cell;
    }
    if (indexPath.section == 0)
    {
        if (indexPath.row == 3 ||indexPath.row == 4+hdINT)
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return [self mCell:cell aCTableView:aTableView cellForRowAtIndexPath:indexPath];
    }
    
    // Configure the cell...
    return cell;
}

- (UITableViewCell *)mCell:(UITableViewCell*)cell aCTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 3 ||indexPath.row == 4+hdINT||indexPath.row == 4)
    {
        int  type = 0;
       if (indexPath.row == 3)
            type =  50;
       else if (indexPath.row == 4)
       {

           NSString *mStr = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"DisplayActivityInfo"]];
           if ([mStr hash] && ![mStr isEqualToString:@"<null>"])
           {
               type = [SendService setString:mStr setSize:labelSize-1 setHight:self.view.bounds.size.width-70];
               if (type < 30) {
                   type = 30;
               }
   
           }
           else
               type = 35;
       }
        else if (indexPath.row == 4+hdINT)
            type =  35;
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10,type -.5, screenMySize.size.width, .5)];
        bgView.backgroundColor = [UIColor colorWithWhite:.8 alpha:1];
        [cell.contentView addSubview:bgView];
    }

    
    if (indexPath.row == 0) {
        NSMutableArray *myArray = [[NSMutableArray alloc] init];
        if ([[allArray allKeys] containsObject:@"PictureModels"])
        {
            for (NSDictionary *pic in [allArray objectForKey:@"PictureModels"])
            {
                [myArray addObject:pic];
            }
        }
        else if ([[allArray allKeys] containsObject:@"DefaultPictureModel"])
        {
            for (NSDictionary *pic in [allArray objectForKey:@"DefaultPictureModel"])
            {
                [myArray addObject:pic];
            }
        }
        
        UIScrollView *ta1ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, screenMySize.size.height*3/5)];
        ta1ScrollView.backgroundColor = [UIColor clearColor];
        [ta1ScrollView setDelegate:self];
        [ta1ScrollView setPagingEnabled:YES];
        ta1ScrollView.tag = scrTag;
        [ta1ScrollView setShowsHorizontalScrollIndicator:NO];
        [cell.contentView addSubview:ta1ScrollView];
        
        [ta1ScrollView setContentSize:CGSizeMake(self.view.bounds.size.width*myArray.count,screenMySize.size.height*3/5)];
        
        UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenMySize.size.height*3/5-70, screenMySize.size.width, 30)];
        pageControl.backgroundColor = [UIColor clearColor];
        pageControl.numberOfPages = myArray.count;
        pageControl.tag = scrTag+1;
        pageControl.currentPage = 0;
        [cell.contentView addSubview:pageControl];
        
        for (int i = 0; i < myArray.count; i ++)
        {
            NSMutableDictionary *dictionary = [myArray objectAtIndex:i];
            
            UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width*i,0, self.view.bounds.size.width, screenMySize.size.height*3/5)];
            [bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dictionary objectForKey:@"FullSizeImageUrl"]]]];
            //            [bgImageView sd_setImageWithURL:[NSURL URLWithString:@"http://172.16.36.241:8061/content/images/thumbs/0001347.jpeg"]];
            //        bgImageView.contentMode =  UIViewContentModeScaleAspectFit;
            bgImageView.contentMode = UIViewContentModeScaleAspectFill;
            bgImageView.clipsToBounds  = YES;
            [ta1ScrollView addSubview:bgImageView];
            
            UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
            CGRect frame = bgImageView.frame;
            [inButton setFrame:frame];
            inButton.tag = scrTag+10+i;
            [inButton addTarget:self action:@selector(aInAction:) forControlEvents:UIControlEventTouchUpInside];
            [ta1ScrollView addSubview:inButton];
        }
        
        
        
        UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0,screenMySize.size.height*3/5-40, self.view.bounds.size.width, 40)];
        barView.backgroundColor = [UIColor colorWithWhite:0 alpha:.3];
        [cell.contentView addSubview:barView];
        
        for (int  i = 0; i < 2; i++)
        {
            UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
            barButton.frame = CGRectMake(0, 0,30, 30);
            barButton.center = CGPointMake(screenMySize.size.width/4+i*screenMySize.size.width/2, 20);
            [barButton addTarget:self action:@selector(baraAction:) forControlEvents:UIControlEventTouchUpInside];
            if (i == 0)
                [barButton setBackgroundImage:[UIImage imageNamed:@"focus"] forState:UIControlStateNormal];
            else if (i == 1)
                [barButton setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
            barButton.tag = baraTagButton +i;
            [barView addSubview:barButton];
            
        }
        aCellShareView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width/2+10,screenMySize.size.height*3/5-100, self.view.bounds.size.width/2-15, 50)];
        aCellShareView.backgroundColor = [UIColor colorWithWhite:0 alpha:.3];
        aCellShareView.alpha = 0;
        [cell.contentView addSubview:aCellShareView];
        
        for (int  i = 0; i < 3; i++)
        {
            UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
            barButton.frame = CGRectMake(0, 0,30, 30);
            barButton.center = CGPointMake((self.view.bounds.size.width/2-15)/6+i*(self.view.bounds.size.width/2-15)/3, 25);
            [barButton addTarget:self action:@selector(barbAction:) forControlEvents:UIControlEventTouchUpInside];
            if (i == 0)
                [barButton setBackgroundImage:[UIImage imageNamed:@"share_weixin"] forState:UIControlStateNormal];
            else if (i == 1)
                [barButton setBackgroundImage:[UIImage imageNamed:@"share_xinlang"] forState:UIControlStateNormal];
            else if (i == 2)
                [barButton setBackgroundImage:[UIImage imageNamed:@"share_xiaonei"] forState:UIControlStateNormal];
            barButton.tag = barbTagButton +i;
            [aCellShareView addSubview:barButton];
            
        }
    }
    else if (indexPath.row == 1)
    {
        cell.textLabel.text = [allArray objectForKey:@"Name"];
        cell.textLabel.numberOfLines = 2;
    }
    else if (indexPath.row == 2)
    {
        int hight = 0;
        NSString *mStr = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"ShortDescription"]];
        if ([mStr hash] && ![mStr isEqualToString:@"<null>"])
        {
            hight = [SendService setString:mStr setSize:labelSize-1 setHight:screenMySize.size.width-40];
            
            [cell.contentView addSubview:[self setFrame:CGRectMake(20, 0, screenMySize.size.width-40, hight) setTitle:mStr setFont:labelSize-1]];
        }

        if ([[NSString stringWithFormat:@"%@",[[allArray objectForKey:@"ProductPrice"] objectForKey:@"OldPrice"]] isEqualToString:@"<null>"]) {

            [cell.contentView addSubview:[self setFrame:CGRectMake(10, hight, screenMySize.size.width-20, 30) setTitle:[[allArray objectForKey:@"ProductPrice"] objectForKey:@"Price"] setFont:labelSize+5]];
        }
        else
        {
            UICustomLineLabel *mlabel = [[UICustomLineLabel alloc] init];
            mlabel.frame = CGRectMake(10, hight, 70, 30);
            mlabel.lineType = LineTypeMiddle;
            mlabel.textColor = [UIColor redColor];
            mlabel.lineColor = [UIColor redColor];
            mlabel.font = [UIFont fontWithName:@"Arial" size:labelSize-2];
            mlabel.text = [[allArray objectForKey:@"ProductPrice"] objectForKey:@"OldPrice"];
            [cell.contentView addSubview:mlabel];
            
            UICustomLineLabel *mlabel1 = [[UICustomLineLabel alloc] init];
            mlabel1.frame = CGRectMake(80, hight, screenMySize.size.width/2, 30);
            mlabel1.lineType = LineTypeNone;
            mlabel1.textColor = [UIColor redColor];
            mlabel1.lineColor = [UIColor redColor];
            mlabel1.font = [UIFont fontWithName:@"Arial" size:labelSize+5];
            mlabel1.text = [[allArray objectForKey:@"ProductPrice"] objectForKey:@"Price"];
            [cell.contentView addSubview:mlabel1];

        }
    }
    else if (indexPath.row == 3 ||indexPath.row == 4 ||indexPath.row == 5||indexPath.row == 5+hdINT)
    {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0, self.view.bounds.size.width-20, 25)];
        titleLabel.textAlignment = 0;
        titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
        [cell.contentView addSubview:titleLabel];
        
        UILabel *title1Label = [[UILabel alloc] initWithFrame:CGRectMake(10,25, self.view.bounds.size.width-20, 25)];
        title1Label.textAlignment = 0;
        title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-3];
        title1Label.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:title1Label];
        
        
        if (indexPath.row == 3)
        {
            titleLabel.textColor = [UIColor grayColor];
            title1Label.textColor = [UIColor grayColor];
            
            titleLabel.text = @"商品评价";
            title1Label.text = [NSString stringWithFormat:@"%@人评论",[[allArray objectForKey:@"ProductReviewOverview"] objectForKey:@"TotalReviews"]];
            title1Label.frame = CGRectMake(95,25, self.view.bounds.size.width-115, 25);
 
            
            float typeF = [[NSString stringWithFormat:@"%@",[[allArray objectForKey:@"ProductReviewOverview"] objectForKey:@"RatingSum"]] floatValue]*75/5;
            TQStarRatingView *starRatingView = [[TQStarRatingView alloc] initWithFrame:CGRectMake(10, 30, 75, 15) numberOfStar:5 initNum:typeF setBool:YES];
            starRatingView.delegate = self;
            [cell.contentView addSubview:starRatingView];
            UIView *tqsView = [[UIView alloc] initWithFrame:starRatingView.frame];
            tqsView.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:tqsView];
        }
        else if (indexPath.row == 4||indexPath.row == 4+hdINT)
        {
            if (hdINT == 0 || indexPath.row == 4+hdINT)
            {
                titleLabel.text = @"已选";
                titleLabel.textColor = [UIColor grayColor];
                titleLabel.frame = CGRectMake(10,5, 40, 25);
                
                title1Label.textColor = [UIColor blackColor];
                title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
                title1Label.frame = CGRectMake(50,5, self.view.bounds.size.width-70, 25);
                title1Label.text = [NSString stringWithFormat:@"%@",selectedString];
            }
            else
            {
                int type = 0;
                NSString *mStr = [NSString stringWithFormat:@"%@",[allArray objectForKey:@"DisplayActivityInfo"]];
                if ([mStr hash] && ![mStr isEqualToString:@"<null>"])
                {
                    type = [SendService setString:mStr setSize:labelSize-1 setHight:self.view.bounds.size.width-70];
                    if (type < 30) {
                        type = 30;
                    }
                    
                    titleLabel.text = @"促销";
                    titleLabel.textColor = [UIColor grayColor];
                    titleLabel.frame = CGRectMake(10,0, 40, type);
                    
                    title1Label.textColor = [UIColor redColor];
                    title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-1];
                    title1Label.frame = CGRectMake(50,5, self.view.bounds.size.width-70, 25);
                    title1Label.text = [NSString stringWithFormat:@"%@",mStr];
                }
            }

        }
        else if (indexPath.row == 5+hdINT)
        {
            titleLabel.textColor = [UIColor grayColor];
            title1Label.textColor = [UIColor blackColor];
            title1Label.font = [UIFont fontWithName:@"Arial" size:labelSize+3];
            titleLabel.text = @"店铺信息";
            title1Label.numberOfLines = 2;
            title1Label.text = [NSString stringWithFormat:@"%@",[[allArray objectForKey:@"VendorModel"] objectForKey:@"Name"]];
            
            UIView *my1View = [[UIView alloc] initWithFrame:CGRectMake(0, 60, screenMySize.size.width, 60)];
            my1View.backgroundColor = bgColor;
            [cell.contentView addSubview:my1View];
            
            UILabel *my1Label = [[UILabel alloc] initWithFrame:CGRectMake(0,20, screenMySize.size.width, 20)];
            my1Label.textAlignment = 1;
            my1Label.text = @"继续拖动，查看图文详情";
            my1Label.textColor = [UIColor blackColor];
            my1Label.font = [UIFont fontWithName:@"Arial" size:labelSize-2];
            [my1View addSubview:my1Label];

        }
    }

    
    return cell;
}

-(UILabel*)setFrame:(CGRect)frame setTitle:(NSString*)title setFont:(int)font
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:frame];
    if (title != nil)
        titleLabel.text = [NSString stringWithFormat:@"%@",title];
    titleLabel.textAlignment = 0;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:font];
    titleLabel.textColor = [UIColor redColor];
    return titleLabel;
}

-(void)gotoSgwc
{
  [self gotoGWCInt];
}

-(void)_sideSlipViewShow
{

    [_sideSlipView show];
}
#pragma mark 点击响应事件
- (void)tableView:(UITableView *)table1View didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 3)
        {
            
        [taScrollView scrollRectToVisible:CGRectMake(0,screenMySize.size.height-40,  screenMySize.size.width, screenMySize.size.height-40) animated:YES];
            [mTableView setType:2];
        }
        else if (indexPath.row == 4+hdINT)
            [_sideSlipView hide];
        else if (indexPath.row == 5+hdINT) {
            ShopsViewController *details = [[ShopsViewController alloc] init];
            details.shopsId = [NSString stringWithFormat:@"%@",[[allArray objectForKey:@"VendorModel"] objectForKey:@"Id"]];
            details.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:details animated:YES];
        }
    }
    
    [table1View deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark 星星
-(void)starRatingView:(TQStarRatingView *)view score:(float)score
{
    NSLog(@"%@",[NSString stringWithFormat:@"%0.2f",score*10]);
}

#pragma mark 返回已选数据
-(void)returnSelected:(int)type setNSM:(NSMutableArray*)apArray
{
    selectArray = apArray;
    selectInt = type;
    selectedString = nil;
    if (apArray.count != 0)
    {
        for (NSDictionary *pic1 in apArray)
        {
            NSLog(@"pic1 %@",pic1);
            NSString * titleString = nil;
            if ([[NSString  stringWithFormat:@"%@",[pic1 objectForKey:@"TextPrompt"]] isEqualToString:@"<null>"]) {
                titleString = [NSString  stringWithFormat:@"%@:",[pic1 objectForKey:@"Name"]];
            }
            else
                titleString = [NSString  stringWithFormat:@"%@:",[pic1 objectForKey:@"TextPrompt"]];
            
            if ([[pic1 allKeys] containsObject:@"Values"])
            {
                for (NSDictionary *pic in [pic1 objectForKey:@"Values"])
                {
                    if ([[NSString stringWithFormat:@"%@",[pic objectForKey:@"IsPreSelected"]] isEqualToString:@"1"]) {
                        titleString = [NSString stringWithFormat:@"%@'%@'",titleString,[pic objectForKey:@"Name"]];
                    }
                }
            }
            if (selectedString == nil) {
                selectedString = titleString;
            }
            else
                selectedString = [NSString stringWithFormat:@"%@,%@",selectedString,titleString];
        }
    }
    if (selectedString == nil) {
        selectedString = [NSString stringWithFormat:@"数量:'%d'",type];
    }
    else
        selectedString = [NSString stringWithFormat:@"%@,数量:'%d'",selectedString,type];
    selectedString = [selectedString stringByReplacingOccurrencesOfString:@"(null)," withString:@""];
    selectedString = [selectedString stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
    [myTableView reloadData];
}

-(void)aInAction:(id)sender
{
    int  type = ((UIButton*)sender).tag-scrTag-10;
    NSMutableArray *myArray = [[NSMutableArray alloc] init];
    if ([[allArray allKeys] containsObject:@"PictureModels"])
    {
        for (NSDictionary *pic in [allArray objectForKey:@"PictureModels"])
        {
            [myArray addObject:pic];
        }
    }
    else if ([[allArray allKeys] containsObject:@"DefaultPictureModel"])
    {
        for (NSDictionary *pic in [allArray objectForKey:@"DefaultPictureModel"])
        {
            [myArray addObject:pic];
        }
    }
    NSMutableArray *photos = [[NSMutableArray alloc] init];
    NSMutableArray *thumbs = [[NSMutableArray alloc] init];
    MWPhoto *photo;
    BOOL enableGrid = YES;
    BOOL startOnGrid = NO;
    
    for (int i = 0; i < myArray.count; i ++)
    {
        NSMutableDictionary *dictionary = [myArray objectAtIndex:i];
        
        photo = [MWPhoto photoWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dictionary objectForKey:@"FullSizeImageUrl"]]]];
        [thumbs addObject:photo];
        photo.caption =nil;
        [photos addObject:photo];
        

    }

    self._photos = photos;
    self._thumbs = thumbs;
    // Create browser
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    browser.displayActionButton = YES;//分享按钮,默认是
    browser.displayNavArrows = YES;//左右分页切换,默认否
    browser.displaySelectionButtons = NO;//是否显示选择按钮在图片上,默认否
    browser.alwaysShowControls = NO;//控制条件控件 是否显示,默认否
    browser.zoomPhotosToFill = YES;//是否全屏,默认是
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
    browser.wantsFullScreenLayout = YES;
#endif
    browser.enableGrid = enableGrid;
    browser.startOnGrid = startOnGrid;
    browser.enableSwipeToDismiss = YES;
    [browser setCurrentPhotoIndex:type];
    
    // Reset selections
    if (photos.count>0) {
        _selections = [NSMutableArray new];
        for (int i = 0; i < photos.count; i++) {
            [_selections addObject:[NSNumber numberWithBool:NO]];
        }
    }
    [self.navigationController pushViewController:browser animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return _photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < _photos.count)
        return [_photos objectAtIndex:index];
    return nil;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
    if (index < _thumbs.count)
        return [_thumbs objectAtIndex:index];
    return nil;
}

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index {
    //    NSLog(@"Did start viewing photo at index %lu", (unsigned long)index);
}

- (BOOL)photoBrowser:(MWPhotoBrowser *)photoBrowser isPhotoSelectedAtIndex:(NSUInteger)index {
    return [[_selections objectAtIndex:index] boolValue];
}

//- (NSString *)photoBrowser:(MWPhotoBrowser *)photoBrowser titleForPhotoAtIndex:(NSUInteger)index {
//    return [NSString stringWithFormat:@"Photo %lu", (unsigned long)index+1];
//}

- (void)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index selectedChanged:(BOOL)selected {
    [_selections replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:selected]];
    //    NSLog(@"Photo at index %lu selected %@", (unsigned long)index, selected ? @"YES" : @"NO");
}

- (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser {
    // If we subscribe to this method we must dismiss the view controller ourselves
    //    NSLog(@"Did finish modal presentation");
    [self dismissViewControllerAnimated:YES completion:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
